package com.laowang.project.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import com.laowang.common.utils.DateUtils;
import com.laowang.common.utils.SecurityUtils;
import com.laowang.framework.redis.RedisCache;
import com.laowang.project.system.domain.*;
import com.laowang.project.system.domain.vo.WxUserVo2;
import com.laowang.project.system.service.MyCyhSocreService;
import com.laowang.project.system.service.UserPointLogService;
import com.laowang.project.system.service.WxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.laowang.project.system.mapper.MyLifeCircleMapper;
import com.laowang.project.system.service.MyLifeCircleService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 我的生活圈Service业务层处理
 *
 * @author laowang
 * @date 2023-04-19
 */
@Service
public class MyLifeCircleServiceImpl implements MyLifeCircleService {
    @Autowired
    private MyLifeCircleMapper myLifeCircleMapper;

    @Autowired
    private MyCyhSocreService myCyhSocreService;

    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private UserPointLogService userPointLogService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询我的生活圈
     *
     * @param id 我的生活圈ID
     * @return 我的生活圈
     */
    @Override
    public MyLifeCircle selectMyLifeCircleById(Integer id) {
        return myLifeCircleMapper.selectMyLifeCircleById(id);
    }

    /**
     * 查询我的生活圈列表
     *
     * @param myLifeCircle 我的生活圈
     * @return 我的生活圈
     */
    @Override
    public List<MyLifeCircle> selectMyLifeCircleList(MyLifeCircle myLifeCircle) {
        return myLifeCircleMapper.selectMyLifeCircleList(myLifeCircle);
    }

    /**
     * 新增我的生活圈
     *
     * @param myLifeCircle 我的生活圈
     * @return 结果
     */
    @Override
    public int insertMyLifeCircle(MyLifeCircle myLifeCircle) {
        Integer mid = SecurityUtils.getLoginUser().getWxUser().getMid();
        myLifeCircle.setUserId(mid);
        myLifeCircle.setCreateTime(DateUtils.getNowDate());
        return myLifeCircleMapper.insertMyLifeCircle(myLifeCircle);
    }

    /**
     * 修改我的生活圈
     *
     * @param myLifeCircle 我的生活圈
     * @return 结果
     */
    @Override
    public int updateMyLifeCircle(MyLifeCircle myLifeCircle) {
        return myLifeCircleMapper.updateMyLifeCircle(myLifeCircle);
    }

    /**
     * 批量删除我的生活圈
     *
     * @param ids 需要删除的我的生活圈ID
     * @return 结果
     */
    @Override
    public int deleteMyLifeCircleByIds(Long[] ids) {
        return myLifeCircleMapper.deleteMyLifeCircleByIds(ids);
    }

    /**
     * 删除我的生活圈信息
     *
     * @param id 我的生活圈ID
     * @return 结果
     */
    @Override
    public int deleteMyLifeCircleById(Integer id) {
        return myLifeCircleMapper.deleteMyLifeCircleById(id);
    }

    @Override
    public List<MyLifeCircle> selectMyLifeCircleListWeb(MyLifeCircle myLifeCircle) {
        return myLifeCircleMapper.selectMyLifeCircleListWeb(myLifeCircle);
    }

    @Override
    public MyLifeCircle selectMyLifeCircleBywebId(Integer id) {

        MyLifeCircle myLifeCircle = myLifeCircleMapper.selectMyLifeCircleBywebId(id);
        if (myLifeCircle!=null){
            //查询这个车友圈评论的人
            List<WxUserVo2> wxUserVo2s = myLifeCircleMapper.selectUserById(id);
            myLifeCircle.setWxUserVo2s(wxUserVo2s);
        }
        return myLifeCircle;
    }

    @Transactional
    @Override
    public Integer addLife(MyLifeCircle myLifeCircle) {

        Integer mid = SecurityUtils.getLoginUser().getWxUser().getMid();
        myLifeCircle.setUserId(mid);
        myLifeCircle.setCreateTime(DateUtils.getNowDate());
        insertMyLifeCircle(myLifeCircle);

        MyCyhSocre myCyhSocre = myCyhSocreService.selectMyCyhSocreByType(3);
        Integer i =null;
        synchronized (this){
            if (myCyhSocre !=null  ){
                Integer score = myCyhSocre.getScore();
                Integer upperLimit = myCyhSocre.getUpperLimit();
                //是否已经满次数了
                CacheObj cacheObj = redisCache.getCacheObject("life:" + mid);
                if (Objects.isNull(cacheObj)){
                    CacheObj cacheObj1 = new CacheObj();
                    cacheObj1.setDate(new Date());
                    cacheObj1.setNum(1);
                    redisCache.setCacheObject("life:"+mid,cacheObj1,2, TimeUnit.DAYS);

                }else {
                    Integer num = cacheObj.getNum();
                    Date date = cacheObj.getDate();
                    Date date1 = new Date();
                    if (DateUtils.isSameDay(date1,date)){
                        if (num.equals(upperLimit)){
                            return 0;
                        }
                        num++;
                        cacheObj.setNum(num);
                        redisCache.setCacheObject("life:"+mid,cacheObj,2, TimeUnit.DAYS);
                    }else {

                        CacheObj cacheObj1 = new CacheObj();
                        cacheObj1.setDate(new Date());
                        cacheObj1.setNum(1);
                        redisCache.setCacheObject("life:"+mid,cacheObj1,2, TimeUnit.DAYS);

                    }
                }

                //加积分
                WxUser wxUser = wxUserService.selectWxUserById(mid);
                Integer points = wxUser.getPoints();
                Integer newPoint = points + score;
                wxUser.setPoints(newPoint);
                wxUserService.updateWxUser(wxUser);
                //积分日志
                UserPointLog userPointLog = new UserPointLog();
                userPointLog.setRemark("发车友圈加积分");
                userPointLog.setOldPoint(points);
                userPointLog.setChangePoint(score);
                userPointLog.setNewPoint(newPoint);
                userPointLog.setUserId(mid);
                userPointLog.setType(3);
                userPointLog.setCreated(new Date());
                userPointLogService.insertUserPointLog(userPointLog);
                i = score;
            }
        }
        return  i;
    }
}
